package cn.com.chengmandian.bike.service.impl;

import cn.com.chengmandian.bike.mapper.EbMemberBlackMapper;
import cn.com.chengmandian.bike.model.EbMemberBlack;
import cn.com.chengmandian.bike.model.EbMemberInfo;
import cn.com.chengmandian.bike.model.EbOperationArea;
import cn.com.chengmandian.bike.model.vo.MemberBlackVo;
import cn.com.chengmandian.bike.model.vo.OpenGetMemberInfoVo;
import cn.com.chengmandian.bike.service.IEbMemberBlackService;
import cn.com.chengmandian.bike.service.IEbMemberInfoService;
import cn.com.chengmandian.bike.service.IEbOperationAreaService;
import cn.com.chengmandian.core.redis.service.RedisService;
import cn.com.chengmandian.core.web.exceptions.BadResponseException;
import cn.com.chengmandian.core.web.pojo.AgentInfo;
import cn.com.chengmandian.core.web.utils.LocalMapUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.MD5;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 电单车运营-用户黑名单表 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2022-01-17
 */
@Service
public class EbMemberBlackServiceImpl extends ServiceImpl<EbMemberBlackMapper, EbMemberBlack> implements IEbMemberBlackService {

    @Autowired
    private IEbMemberInfoService memberInfoService;
    @Autowired
    private IEbOperationAreaService operationAreaService;
    @Autowired
    private RedisService redisService;

    @Override
    public void checkPhone(String phone){

        MemberBlackVo ebMemberBlack = baseMapper.getPhoneData(phone);
        if(ebMemberBlack != null){
            ebMemberBlack.setCodeMessage(this.getOuterLoginCode(ebMemberBlack.getMemberId()));
        }
        if(ebMemberBlack != null && ebMemberBlack.getExpireTime() != null){
            ebMemberBlack.setOutTime(DateUtil.format(ebMemberBlack.getExpireTime(),"yyyy-MM-dd"));
        }
        if(ebMemberBlack != null && ebMemberBlack.getExpireTime() == null){
            throw new BadResponseException(506,"手机号【"+phone+"】已被拉黑,请联系客服!",ebMemberBlack);
        }
        if(ebMemberBlack != null && ebMemberBlack.getExpireTime() != null && (new Date()).getTime() <=  ebMemberBlack.getExpireTime().getTime()){
            throw new BadResponseException(506,"手机号【"+phone+"】在【"+ DateUtil.format(ebMemberBlack.getExpireTime(),"yyyy-MM-dd")+"】之前已被禁止使用,请联系客服!",ebMemberBlack);
        }
    }

    @Override
    public void checkIdCard(String idCard) {
        MemberBlackVo ebMemberCardBlack = baseMapper.getIdCardData(idCard);
        if(ebMemberCardBlack != null){
            ebMemberCardBlack.setCodeMessage(this.getOuterLoginCode(ebMemberCardBlack.getMemberId()));
        }
        if(ebMemberCardBlack != null && ebMemberCardBlack.getExpireTime() != null){
            ebMemberCardBlack.setOutTime(DateUtil.format(ebMemberCardBlack.getExpireTime(),"yyyy-MM-dd"));
        }
        if(ebMemberCardBlack != null && ebMemberCardBlack.getExpireTime() == null){
            throw new BadResponseException(506,"身份证号【"+idCard+"】已被拉黑,请联系客服!",ebMemberCardBlack);
        }
        if(ebMemberCardBlack != null && ebMemberCardBlack.getExpireTime() != null && (new Date()).getTime() <=  ebMemberCardBlack.getExpireTime().getTime()){
            throw new BadResponseException(506,"身份证号【"+idCard+"】在【"+ DateUtil.format(ebMemberCardBlack.getExpireTime(),"yyyy-MM-dd")+"】之前已被禁止使用,请联系客服!",ebMemberCardBlack);
        }
    }

    private String getOuterLoginCode(Integer memberId){
        if(memberId == null) return "";
        AgentInfo agentInfo = LocalMapUtil.getAgentInfo();
        EbMemberInfo memberInfo = memberInfoService.getById(memberId);
        OpenGetMemberInfoVo memberInfoVo = new OpenGetMemberInfoVo();
        memberInfoVo.setUuid(MD5.create().digestHex("EB" + agentInfo.getId() + "-" + memberInfo.getId()));
        memberInfoVo.setName(memberInfo.getName());
        memberInfoVo.setMobile(memberInfo.getMobile());
        memberInfoVo.setSex(memberInfoVo.getSex());

        if (agentInfo.getAreaId() != null) {
            EbOperationArea operationArea = operationAreaService.getById(agentInfo.getAreaId());
            if (operationArea != null) {
                memberInfoVo.setAreaName(operationArea.getName());
            }
        }

        String code = RandomUtil.randomString(32);
        String key = "CMD:BIKE:MEMBER:OPEN:" + code;
        redisService.set(key, memberInfoVo, 60 * 10);
        return code;
    }
}
